/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataset;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import parse.CCGDerivation;
import parse.GMBXMLParser;

/**
 *
 * @author phong
 */
public class GMBReader {
    
    int sub_id;
    int subsub_id;
    
    String[] subnames;
    String path;
    
    public void open(String dirPath) {
        try {
            path = dirPath + "/data";
            File dir = new File(path);
            subnames = dir.list();
            
            sub_id = 0;
            subsub_id = 0;
            
        } catch (Exception e) {
            e.printStackTrace();
        }                
    }
    
    public boolean hasNext() {
        return sub_id < subnames.length;
    }
    
    public List<CCGDerivation> next() {
        
        if (sub_id == subnames.length)
            return null;
        
        GMBXMLParser xmlParser = new GMBXMLParser();        
        List<CCGDerivation> ders = new LinkedList<CCGDerivation>();
        String subname = subnames[sub_id];
        
        try {
            File subdir = new File(path + "/" + subname);
            String[] subsubnames = subdir.list();
            
            String subsubname = subsubnames[subsub_id];
            
            // reading CCG derivations
            File file = new File(path + "/" + subname + "/" + subsubname + "/en.der.xml");

            System.out.println("--------------------------------------------------------");
            System.out.println(subname + "/" + subsubname);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            dbf.setValidating(false);
            dbf.setFeature("http://xml.org/sax/features/namespaces", false);
            dbf.setFeature("http://xml.org/sax/features/validation", false);
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();

            NodeList childNodes = ((Element)doc.getElementsByTagName("xdrs-output").item(0)).getElementsByTagName("der"); 

            for (int i = 1; i <= childNodes.getLength(); i++) {
                Element elm = (Element)childNodes.item(i-1);
                try {
                    CCGDerivation der = xmlParser.parseDerivation(elm);
                    ders.add(der);
                }
                catch(Error e) {
                    e.printStackTrace();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }

            subsub_id++;
            if (subsub_id == subsubnames.length) {
                subsub_id = 0;
                sub_id++;
            }
            
            return ders;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }        
    }    
}
